import Vue from 'vue'
import VueRouter from 'vue-router'
import two_router from '../components/two_router'
import home from '../components/home'
import my from '../components/my'
import news from '../components/news'
import msg from '../components/msg'
import search from '../components/search'
import login from '../components/login'
import set from '../components/set'
import user_safe from '../components/user_safe'
import set_phone from '../components/set_phone'
import set_password from '../components/set_password'
import set_email from '../components/set_email'
import user_userinfo from '../components/user_userinfo'
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}
Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        redirect: '/two_router',
    },
    {
        name: 'search',
        path: '/search',
        component: search,
    },
    {
        name: 'set_email',
        path: '/set_email',
        component: set_email,
    },
    {
        name: 'user_userinfo',
        path: '/user_userinfo',
        component: user_userinfo,
    },
    {
        name: 'set_phone',
        path: '/set_phone',
        component: set_phone,
    },
    {
        name: 'set_password',
        path: '/set_password',
        component: set_password,
    },
    {
        name: 'user_safe',
        path: '/user_safe',
        component: user_safe,
    },
    {
        name: 'set',
        path: '/set',
        component: set,
    },
    {
        name: 'login',
        path: '/login',
        component: login,
    },
    {
        name: 'two_router',
        path: '/two_router',
        component: two_router,
        redirect: '/home',
        children: [
            {
                name: 'home',
                path: '/home',
                component: home,
            },
            {
                name: 'my',
                path: '/my',
                component: my,
            },
            {
                name: 'news',
                path: '/news',
                component: news,
            },
            {
                name: 'msg',
                path: '/msg',
                component: msg,
            },
        ]
    }
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})
router.beforeEach((to, from, next) => {
    if (to.path == '/car') {
        let data = localStorage.getItem('token')
        if (data) {
            next()
        } else {
            next('/login')
        }
    }
    next()
})
export default router
